var partNum;
$(document).ready(function(){
	$.ajax({
		url:"../reloadTestInf",
		type:"post",
		data:{
			testid:getQueryString("test_id"),
			partNo:0,
			questionNo:0,
			type:1,
		},
		async: false,
		success:function(res){
			partNum=res.partNum;
			$("#partNum").val(partNum);
			$("#totalPoint").val(res.totalPoint);
			$("#testName").html(res.testName);
		}
	});	
	creatTestInf();
	setPart();
});

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function creatTestInf(){
	partNum=$("#partNum").val();
	$("#parts").empty();
	for(var i=1;i<=partNum;i++){
		$.ajax({
			url:"htmlPart/parts.html",
			type:"post",
			data:{},
			async: false,
			dataType: "html",
			success:function(res){
				$("#parts").append($(res));
				
				$("#partNo").html("第"+i+"部分");
				$("#partNo").attr("id","partNo"+i);
				$("#partNo"+i).attr("name",i);
				
				$("#partName").attr("id","partName"+i);
				$("#typeList").attr("id","typeList"+i);
				$("#questionNum").attr("id","questionNum"+i);
				$("#perQuestionPoint").attr("id","perQuestionPoint"+i);
				
				$("#questions").attr("name",i);
				$("#questions").attr("id","questions"+i);			
				
			}
		});	
	}
};

function setPart(){
	partNum=$("#partNum").val();
	$("#testName").attr("disabled","disabled");
	$("#totalPoint").attr("disabled","disabled");
	$("#partNum").attr("disabled","disabled");

	for(var i=1;i<=partNum;i++){
		$.ajax({
			url:"../reloadTestInf",
			type:"post",
			data:{
				testid:getQueryString("test_id"),
				partNo:i,
				questionNo:0,
				type:2,
			},
			async: false,
			success:function(res){
				$("#partName"+i).val(res.partName);
				$("#typeList"+i).val(res.typeList);
				$("#questionNum"+i).val(res.questionNum);
				$("#perQuestionPoint"+i).val(res.perPoint);
			}
		});
		$("#partName"+i).attr("disabled","disabled");
		$("#typeList"+i).attr("disabled","disabled");
		$("#questionNum"+i).attr("disabled","disabled");
		$("#perQuestionPoint"+i).attr("disabled","disabled");
		
		addTopic(i,$("#questionNum"+i).val());
		
	}  
	
}

function addTopic(partNo,questionNum){
	if($("#typeList"+partNo).val()==1){
		for(var i=1;i<=questionNum;i++){
			addSingle(partNo,i);
		}	
	}else if($("#typeList"+partNo).val()==2){
		for(var i=1;i<=questionNum;i++){
			addMultiple(partNo,i);
		}
	}else if($("#typeList"+partNo).val()==3){
		for(var i=1;i<=questionNum;i++){
			addSubject(partNo,i);
		}
	}
	
}

function addSingle(partNo,questionNo){
	$.ajax({
		url:"htmlPart/single.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 单选";
			
			$("#new").addClass("1");
			$("#new").attr("id",partNo+"topic"+questionNo);

			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			
			setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。

			$("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");

			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
					loadBeforeResult(partNo,questionNo,res.questionId,1)
				}
			});
		}
	});		
}

function addMultiple(partNo,questionNo){
	$.ajax({
		url:"htmlPart/multiple.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 多选";
			
			$("#new").addClass("2");
			$("#new").attr("id",partNo+"topic"+questionNo);

            $("#question").attr("id",partNo+"topic"+questionNo+"q");
            
            setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。

            $("#A").attr("id",partNo+"topic"+questionNo+"A");
			$("#B").attr("id",partNo+"topic"+questionNo+"B");
			$("#C").attr("id",partNo+"topic"+questionNo+"C");
			$("#D").attr("id",partNo+"topic"+questionNo+"D");

			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			var checks=document.getElementsByName("checks");
			for(var i=0;i<4;i++){
				checks[0].setAttribute("name",partNo+"topic"+questionNo+"answer");
			}
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
					loadBeforeResult(partNo,questionNo,res.questionId,2)
				}
			});
		
		}
	});		
};
function addSubject(partNo,questionNo){
	$.ajax({
		url:"htmlPart/subject.html",
		type:"get",
		data:{},
		dataType: "html",
		success:function(res){
			$("#questions"+partNo).append($(res));
			
			var body=document.getElementById("new");
			var children=body.childNodes;
			children[1].innerHTML="第"+questionNo+"题 主观题";
			
			$("#new").addClass("3");
			$("#new").attr("id",partNo+"topic"+questionNo);

			$("#question").attr("id",partNo+"topic"+questionNo+"q");
			
			setSearchCard(partNo,questionNo,$("#typeList"+partNo).val());//为搜索区域的三个按钮添加功能。

			$("#answer").attr("id",partNo+"topic"+questionNo+"a");

			$("#setSubject").attr("id",partNo+"topic"+questionNo+"setSubject");
			$("#setKnowledgePoint").attr("id",partNo+"topic"+questionNo+"setKnowledgePoint");
			
			$.ajax({
				url:"../reloadTestInf",
				type:"post",
				data:{
					testid:getQueryString("test_id"),
					partNo:partNo,
					questionNo:questionNo,
					type:3,
				},
				async: false,
				success:function(res){
					loadBeforeResult(partNo,questionNo,res.questionId,3)
				}
			});
			
		}
	});		
}

function loadBeforeResult(partNo,questionNo,questionId,type){
	$.ajax({
		url:"../loadQuestionInf",
		type:"post",
		data:{
			questionId:questionId,
			type:type,
		},
		async: false,
		success:function(res){		
			if(type==1){
				if(res.topic=="null"){
					$("#"+partNo+"topic"+questionNo+"q").val("");
					$("#"+partNo+"topic"+questionNo+"A").val("");
					$("#"+partNo+"topic"+questionNo+"B").val("");
					$("#"+partNo+"topic"+questionNo+"C").val("");
					$("#"+partNo+"topic"+questionNo+"D").val("");
					$("#"+partNo+"topic"+questionNo+"setSubject").val("");
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val("");
				}else {
					$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
					$("#"+partNo+"topic"+questionNo+"A").val(res.A);
					$("#"+partNo+"topic"+questionNo+"B").val(res.B);
					$("#"+partNo+"topic"+questionNo+"C").val(res.C);
					$("#"+partNo+"topic"+questionNo+"D").val(res.D);
					$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}
				
				
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='"+res.answer+"']").attr("checked",true);
				
				
			}else if(type==2){
				if(res.topic=="null"){
					$("#"+partNo+"topic"+questionNo+"q").val("");
					$("#"+partNo+"topic"+questionNo+"A").val("");
					$("#"+partNo+"topic"+questionNo+"B").val("");
					$("#"+partNo+"topic"+questionNo+"C").val("");
					$("#"+partNo+"topic"+questionNo+"D").val("");
					$("#"+partNo+"topic"+questionNo+"setSubject").val("");
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val("");
				}else {
					$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
					$("#"+partNo+"topic"+questionNo+"A").val(res.A);
					$("#"+partNo+"topic"+questionNo+"B").val(res.B);
					$("#"+partNo+"topic"+questionNo+"C").val(res.C);
					$("#"+partNo+"topic"+questionNo+"D").val(res.D);
					$("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}
				
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",false);
				$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",false);

				if(res.answer_A!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='A']").attr("checked",true);}
				if(res.answer_B!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='B']").attr("checked",true);}
				if(res.answer_C!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='C']").attr("checked",true);}
				if(res.answer_D!="null"){$("input[name='"+partNo+"topic"+questionNo+"answer"+"'][value='D']").attr("checked",true);}

			}else if(type==3){
				if(res.topic=="null"){
					$("#"+partNo+"topic"+questionNo+"q").val("");
					$("#"+partNo+"topic"+questionNo+"a").val("");
	                $("#"+partNo+"topic"+questionNo+"setSubject").val("");
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val("");
				}else {
					$("#"+partNo+"topic"+questionNo+"q").val(res.topic);
					$("#"+partNo+"topic"+questionNo+"a").val(res.answer);
	                $("#"+partNo+"topic"+questionNo+"setSubject").val(res.subject);
					$("#"+partNo+"topic"+questionNo+"setKnowledgePoint").val(res.knowledgePoint);
				}
				
			}
		}
	});
}
